package com.beemans.admob.demo.ui.fragments

import android.util.Log
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.admob.ads.AbAdvertManager
import com.beemans.admob.ads.AbSplashAdvert
import com.beemans.admob.ads.AbSplashAdvertCallback
import com.beemans.admob.ads.AbSplashAdvertConfig
import com.beemans.admob.demo.R
import com.beemans.admob.demo.constant.Constant
import com.beemans.admob.demo.databinding.FragmentSplashBinding
import com.google.android.gms.ads.ResponseInfo
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2020/7/9.
 */
class SplashAdFragment : CommonFragment() {
    private val dataBinding: FragmentSplashBinding? by lazyDataBindingOrNull()

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_splash)
    }

    override fun initEvent() {
        // 加载广告
        dataBinding?.splashAdBtnPreload?.clickNoRepeat {
            preloadAdvert()
        }

        // 展示广告
        dataBinding?.splashAdBtnLoad?.clickNoRepeat {
            showAdvert()
        }
    }

    /**
     * 加载广告
     */
    private fun preloadAdvert() {
        val advertConfig = AbSplashAdvertConfig(Constant.SPLASH_ID)

        AbAdvertManager.loadSplashAdvert(
            this,
            advertConfig,
            object : AbSplashAdvertCallback() {
                override fun onAdvertLoaded(abSplashAdvert: AbSplashAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(abSplashAdvert: AbSplashAdvert, adError: String?) {
                    printLog("广告加载失败 --- adError:$adError")
                }
            }
        )
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = AbSplashAdvertConfig(Constant.SPLASH_ID)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = true

        AbAdvertManager.loadSplashAdvert(
            this,
            advertConfig,
            object : AbSplashAdvertCallback() {
                override fun onAdvertLoaded(splashAdvert: AbSplashAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(splashAdvert: AbSplashAdvert, adError: String?) {
                    printLog("广告加载失败 --- adError:$adError")
                }

                override fun onAdvertShow(
                    splashAdvert: AbSplashAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告展示 --- responseInfo:$responseInfo")
                }

                override fun onAdvertClicked(
                    splashAdvert: AbSplashAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告点击 --- responseInfo:$responseInfo")
                }

                override fun onAdvertClose(
                    splashAdvert: AbSplashAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告关闭 --- responseInfo:$responseInfo")
                }
            }
        )
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}